<?php
namespace Admin\Controller;

class PriceController extends AdminController
{
   public function index(){
        $name          = I('get.name');
        $map['status'] = array('egt', 0);
        if (is_numeric($name)) {
            $map['id|name'] = array(intval($name), array('like', '%' . $name . '%'), '_multi' => true);
        } else {
            $map['name'] = array('like', '%' . (string) $name . '%');
        }
       $res = $this->lists('attr', $map);
        // $res=M('attr')->select();
       $this->assign('res',$res);
       $this->display();
  }
   
    public function add(){
  	    
    if(IS_GET){
  	     $gid=I('get.id');
  	     $goodname=I('get.goodname');
         $res=M('attrval')->where(['gid'=>$gid])->select();
         $keyname=M('attrkey')->where(['gid'=>$gid])->select();
          // dump($keyname);
         $kid=M('attrkey')->where(['gid'=>$gid])->getField('id,keyname');
         // dump($kid);//$kid得到定制商品属性名称和id
         
          //dump($dl);//$dl遍历出来属性值vname交配结果
         $arr=array();
         foreach ($kid as $k => $v) {
			$arr[]=M('attrval')->where(['kid'=>$k])->getField('id,vname');
         }
         $dl=$this->CartesianProduct($arr);
          //$dl2为了获得商品属性值笛卡尔交配集（属性值id）
         $arr1=array();
         //var_dump($arr);
         foreach ($arr as $ka => $va) {
         	$arr1[]=array_keys($va);
         }
         $dl2=$this->CartesianProduct($arr1);
         //dump($dl);
         // dump($dl2);
         $array=array_combine($dl2, $dl);
         //dump($array);
         $this->assign('gid',$gid);
         $this->assign('goodname',$goodname);
         $this->assign('dl',$dl);
         $this->assign('abc',$array);
         $this->assign('dl2',$dl2);
         $this->assign('keyname',$keyname);
         $this->display(); 

      }else{

      	$post =I('post.');

      	$arr =[];
      	dump($post['price']);
      	foreach ($post['price'] as $k => $v) {
      		$tmp=[];
      		$tmp['price'] =$v;
      		$tmp['num']=$post['num'][$k];
      		$tmp['attrvid']=$post['attrvid'][$k];
      		$tmp['attrname']=$post['attrname'][$k];
      		$tmp['gid']=$post['gid'];
      		$tmp['goodname']=$post['goodname'];
      		$arr[]=$tmp;
      		// dump($tmp['num']);
      		// dump($v);die;
      	}

      	// dump($arr);
      	$jiage=M('attr')->addAll($arr);
      	if ($jiage) {
                $this->success('定制商品价格成功', U('index'));
            } else {
                $this->error('定制商品价格成功');
            }
      }
}

    public function update(){

     if(IS_GET){
       $id=I('get.id');
       $res=M('attr')->where(['id'=>$id])->select();
       // dump($res);
       $goodname=$res[0]['goodname'];
       $attrvid=$res[0]['attrvid'];
       $attrname=$res[0]['attrname'];
       $gid=$res[0]['gid'];
       $this->assign('id',$id);
       $this->assign('gid',$gid);
       $this->assign('goodname',$goodname);
       $this->assign('attrvid',$attrvid);
       $this->assign('attrname',$attrname);
       $this->assign('res',$res);
       $this->display();
     }else{
          dump(I('post.'));
          $post=I('post.');
          $data=M('attr')->where(['id'=>I('post.ooid')])->save($post);
           // dump(M('attr')->getLastSql());die;
         
            if ($data) {
               
                $this->success('修改价格成功', U('index'));
            } else {
                $this->error('修改价格失败');
            }
     }

 }


    public function CartesianProduct($sets){
 // 保存结果
 $result = array();
 // 循环遍历集合数据
 for($i=0,$count=count($sets); $i<$count-1; $i++){
 // 初始化
 if($i==0){
  $result = $sets[$i];
 }
 // 保存临时数据
 $tmp = array();
 // 结果与下一个集合计算笛卡尔积
 foreach($result as $res){
  foreach($sets[$i+1] as $set){
  $tmp[] = $res.','.$set;
  }
 }
 // 将笛卡尔积写入结果
 $result = $tmp;
 }
 return $result;
 
}

    public function changeStatus($method = null)
    {
        $id = array_unique((array) I('id', 0));

        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        switch (strtolower($method)) {
            case 'del':
                M('attr')->where($map)->delete();
                $this->success('删除成功');
                break;
            default:
                $this->error('参数非法');
        }
    }



}